package br.perfiman.service.impl;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import br.ce.wcaquino.Validation.GeneralValidation;
import br.perfiman.dao.AbstractDAOService;
import br.perfiman.dao.AccountDAOService;
import br.perfiman.exceptions.ErrorsConstants;
import br.perfiman.exceptions.InvalidFieldException;
import br.perfiman.exceptions.RequiredFieldException;
import br.perfiman.model.Account;
import br.perfiman.service.AccountService;

/**
 *
 * @author Wagner Costa
 * @since 27/12/2007
 * @version 1.0
 *
 */
@Stateless
public class AccountServiceBean extends AbstractServiceBean<Account> implements AccountService{
	@EJB(name="AccountDAOService")
	protected AccountDAOService dao;

	@Override
	public AbstractDAOService<Account> getDao() {
		return dao;
	}

	@Override
	public Account insert(Account entity) throws Exception {
		return super.insert(entity);
	}

	@Override
	public void remove(Account entity) throws Exception {
		super.remove(entity);
	}

	@Override
	public List<Account> retrieveAll() throws Exception {
		return super.retrieveAll();
	}

	@Override
	public Account retrieveById(Object id) throws Exception {
		return super.retrieveById(id);
	}

	@Override
	public Account update(Account entity) throws Exception {
		return super.update(entity);
	}

	@Override
	public void validateBussinessRules(Account entity, boolean isUpdate){
		// TODO Auto-generated method stub

	}

	@Override
	public void validateInvalidFields(Account entity, boolean isUpdate) {
		if(entity.getInitialValue() < 0)
			throw new InvalidFieldException(getFormKey("initialValue"), ErrorsConstants.GREATER_THAN, "0");
	}

	@Override
	public void validateRequiredFields(Account entity, boolean isUpdate) {
		RequiredFieldException exception = new RequiredFieldException();
		if(GeneralValidation.isEmpty(entity.getName()))
			exception.add(getFormKey("name"), ErrorsConstants.REQUIRED);
		if(GeneralValidation.isEmpty(entity.getInitialValue()))
			exception.add(getFormKey("initialValue"), ErrorsConstants.REQUIRED);

		if(exception.hasErrors())
			throw exception;
	}

}
